#!/usr/bin/perl -w

use warnings;
use strict;

use MyApp::Schema;
use Data::Dumper;


sub do_connect {
    my $db_file = @_;
    return my $schema = MyApp::Schema->connect("dbi:SQLite:../mmorpg_db1.db");
}

sub pop_account {
    # data account
    my $acc_data = [
	{
	    acct_name => 'test0',
	    password => 'test0',
	    subscriber_name => 'Test ZERO',
	    subscriber_email => 'test0@test.server.com',
	    subscriber_addr => 'zip 000000000000',
	},
	{
	    acct_name => 'test1',
	    password => 'test1',
	    subscriber_name => 'Test ONE',
	    subscriber_email => 'test1@test.server.com',
	    subscriber_addr => 'zip 111111111111',
	},
	{
	    acct_name => 'test2',
	    password => 'test2',
	    subscriber_name => 'Test TWO',
	    subscriber_email => 'test2@test.server.com',
	    subscriber_addr => 'zip 222222222222',
	}
    ];

    my $schema = do_connect();
    $schema->resultset('Account')->populate($acc_data) ||
	die "Couldn't populate account table \n";
}

sub pop_creep {
    # creep data
    my $creep_data = [
	{ 
	    name => 'creep_test0',
	    hit_pts => 10,
	    mana => 100,
	    attack => 40,
	},
	{ 
	    name => 'creep_test1',
	    hit_pts => 10,
	    mana => 100,
	    attack => 40,
	},
	{ 
	    name => 'creep_test2',
	    hit_pts => 10,
	    mana => 100,
	    attack => 40,
	},
    ];

    my $schema = do_connect();
    $schema->resultset('Creep')->populate( $creep_data );
}

sub pop_item {
    my $item_data = [
	{
	    name => 'item_test0',
	    item_type => 'A',
	    item_damage => 20,
	},
	{ 
	    name => 'item_test1',
	    item_type => 'B',
	    item_damage => 15,
	},
	{ 
	    name => 'item_test2',
	    item_type => 'C',
	    item_damage => 10,
	},
    ];

    my $schema = do_connect();

    $schema->resultset('Item')->populate($item_data);
}

sub fetch_modify {
    my $schema = do_connect();

    # search row data
    my $acc_rs = $schema->resultset('Account')->find({acct_name => 'test1'});

    return if $acc_rs == 0;

    # modify the row
    $acc_rs->subscriber_email('test1@test.server.com');
    $acc_rs->update;
}


sub pop_char {
    my $schema = do_connect();
    my $char_data = [
	{
	    char_name => 'char_test0',
	    char_level => 0,
	    char_type => 'A',
	    exp_points => 100,
	    max_hit_pts => 300,
	    max_mana => 200,
	    curr_hit_pts => 10,
	    curr_mana => 5,
	    reg_name => $schema->resultset('Region')->find_or_create(
		reg_name => 'region_test0',
	    )->reg_name,
	    acct_name => $schema->resultset('Account')->find_or_create(
		acct_name => 'test0'
	    )->acct_name,
	},
	{
	    char_name => 'char_test1',
	    char_level => 1,
	    char_type => 'B',
	    exp_points => 90,
	    max_hit_pts => 200,
	    max_mana => 100,
	    curr_hit_pts => 8,
	    curr_mana => 3,
	    reg_name => $schema->resultset('Region')->find_or_create(
		reg_name => 'region_test1',
	    )->reg_name,
	    acct_name => $schema->resultset('Account')->find_or_create(
		acct_name => 'test1',
	    )->acct_name,
	},
	{
	    char_name => 'char_test2',
	    char_level => 2,
	    char_type => 'C',
	    exp_points => 100,
	    max_hit_pts => 300,
	    max_mana => 200,
	    curr_hit_pts => 10,
	    curr_mana => 1,
	    reg_name => $schema->resultset('Region')->find_or_create(
		reg_name => 'region_test2',
	    )->reg_name,
	    acct_name => $schema->resultset('Account')->find_or_create(
		acct_name => 'test2'
	    )->acct_name,

	}
    ];

    $schema->resultset('CharacterT')->populate($char_data);
}

sub pop_region {
    my $region_data = [
	{
	    reg_name => 'region_test0',
	    climate => 'climate_test0',
	    precipitation => 0,
	    foliage => 0,
	},
	{
	    reg_name => 'region_test1',
	    climate => 'climate_test1',
	    precipitation => 1,
	    foliage => 1,
	},
	{
	    reg_name => 'region_test2',
	    climate => 'climate_test2',
	    precipitation => 2,
	    foliage => 2,
	},
    ];

    my $schema = do_connect();
    $schema->resultset('Region')->populate($region_data);
}

sub pop_ran_into {
    my $schema = do_connect();
    my $char = $schema->resultset('CharacterT');
    my $creep = $schema->resultset('Creep');

    my $ran_into_data = [
	{
	    char_name_fk => $char->find_or_create( { char_name => 'test0' } )->char_name,
	    creep_id_fk => $creep->find_or_create( { name => 'creep_test0' })->name,
	},
	{
	    char_name_fk => $char->find_or_create( { char_name => 'test0' } )->char_name,
	    creep_id_fk => $creep->find_or_create( { name => 'creep_test1' })->name,
	},
	{
	    char_name_fk => $char->find_or_create( { char_name => 'test1' } )->char_name,
	    creep_id_fk => $creep->find_or_create( { name => 'creep_test0' })->name,
	},
    ];
    
    $schema->resultset('RanInto')->populate($ran_into_data);
}

sub pop_creep_instantiation {
    my $schema = do_connect;

    my $creep_instantiation_data = [
	{
	    creep_id_fk => 'creep_test0',
	    region_fk => 'region_test0',
	},
	{
	    creep_id_fk => 'creep_test1',
	    region_fk => 'region_test0',
	},
	{
	    creep_id_fk => 'creep_test0',
	    region_fk => 'region_test1',
	},
    ];

    $schema->resultset('CreepInstantiation')->populate($creep_instantiation_data);
}

sub pop_item_instantiation {
    my $schema = do_connect;

    my $item_instantiation_data = [
	{
	    modifier => "+2",
	    char_name_fk => 'char_test0',
	    creep_id_fk => 1,
	    item_id_fk => 1,
	},
    ];

    $schema->resultset('ItemInstantiation')->populate($item_instantiation_data);
}

pop_account;
pop_region;
pop_creep;
pop_creep_instantiation
pop_item;
pop_item_instantiation;
pop_char;
pop_ran_into;
